home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4616 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.4 KB

  1. Path: newspost1.alt.net!usenet
  2. From: walth@netcom.com (Walt Howard)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to handle error in constructor
  5. Date: Wed, 31 Jan 1996 09:14:27 GMT
  6. Organization: AltNet - Affordable Usenet Access - http://www.alt.net
  7. Message-ID: <4enbts$35e@tofu.alt.net>
  8. References: <DLyyIM.5EG@teslab.lab.oz.au>
  9. Reply-To: walth@netcom.com
  10. X-Newsreader: Forte Agent .99c/32.126
  11.  
  12. On Tue, 30 Jan 1996 00:54:21 GMT, andrew@teslab.lab.oz.au (Andrew
  13. Phillips) wrote:
  14.  
  15. >I'm converting and enhancing a simple program in C to C++.  Without going
  16. >into too much detail I have a class that represents a file on disk.  The
  17. >contructor opens the file, but what should it do if the file cannot be
  18. >opened?  The C program just calls fopen() tests the return value and 
  19. >if there's an error it prints a message and exits.
  20. >
  21. >In C++ I thought to set a flag in the constructor and have a member function
  22. >that tests the flag to see if the file was opened correctly.  This seems
  23. >rather inelegant -- I guess exceptions would be the elegant way but seem
  24. >like overkill for such a simple program.  I've looked in several C++ books but
  25. >error handling is given scant coverage except for exception handling.
  26. >
  27. >Any suggestions would be greatly appreciated.
  28. >
  29.  
  30. There are several approaches. The one you came up with is similar to
  31. what the C++ stream library does. However, that was written before
  32. exceptions were available. 
  33.  
  34. Exceptions is the only way to go on this. And you shouldn't consider
  35. them overkill, they really aren't. All you have to say is, for
  36. example:
  37.  
  38. try
  39. {
  40.     if ( action() == fail )
  41.         throw( "dang file didn't open" ); 
  42.             //Here I "throw" a char* pointer
  43. }
  44. catch( char* errorMessage ) // and here I catch it and deal with it
  45. {
  46.     printf( "%s", errorMessage );
  47.     exit( -1 ); // or whatever
  48. }
  49.  
  50. Try it a few times and you'll see how simple it is. The books make it
  51. seem complicated but it isn't. The rewards are well worth it. 
  52.  
  53. Exceptions will reliably trap and clean up a bad construction and I
  54. recommend using them as such, however there are some other issues
  55. involved.
  56.  
  57. From my experience, I would say that any class whose constructor has
  58. ANY chance to fail, should not do the failure-possible action during
  59. the constructor. Have a function called init(void) (always void
  60. argument) that does the actual activation of the object.
  61.  
  62. Have the constructor do EVERYTHING necessary to prime/lock and load
  63. the object, but the init( ) function actually pulls the trigger. The
  64. init() function should just have (void) as an argument because all its
  65. going to do is pull the trigger right?
  66.  
  67. For a file object, the construction might set the name and file access
  68. mode but the init() function would actually OPEN the file.
  69.  
  70. This has many benefits. For example, if you're object's constructor
  71. can fail, GLOBAL objects (those constructed at startup time) can fail
  72. and you can't trap that with an try/catch exception handler. Way
  73. bummer.
  74.  
  75. There are other benefits but I think you'll see this way is good after
  76.  
  77. some experience.
  78.  
  79. Plus, you can always overload another construction to automatically
  80. call the init() function also to get the functionality of the FULL
  81. construction if you want. 
  82.  
  83. -- 
  84. >Andrew Phillips (News/Sys Admin)  andrew@teslab.lab.oz.au  +61 2 287 6551
  85. >--------------------------------
  86. >Just a SPOKE, not a SPOKESPERSON
  87.  
  88. //////////////////////////////////////////////////////////
  89. C++ programmers like to see the forest. C programmers like 
  90. to see the trees.
  91.  
  92. Walt Howard
  93.  
  94.